package com.ctgu.springshiro.controller;

import com.ctgu.springshiro.entity.User;
import com.ctgu.springshiro.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
public class LoginController {

    @Autowired
    private IUserService IUserService;

    @Autowired
    private Environment env;

    @RequiresRoles("admin")
    @RequiresPermissions("add")
    @GetMapping("/")
    public User index() {
        User user = IUserService.findUserById(1);
        System.out.println(user);
        return user;
    }

    @GetMapping(value = {"/to/login", "/unauth"})
    public String toLogin() {
        return "login";
    }

    @PostMapping(value = "/login")
    public String login(@RequestParam String username, @RequestParam String password, ModelMap modelMap) {
        String errorMsg = "";
        try {
            if (!SecurityUtils.getSubject().isAuthenticated()) {
                String newPsd = new Md5Hash(password, env.getProperty("shiro.encrypt.password.salt")).toString();
                UsernamePasswordToken token = new UsernamePasswordToken(username, newPsd);
                SecurityUtils.getSubject().login(token);
            }
        } catch (UnknownAccountException e) {
            errorMsg = e.getMessage();
            modelMap.addAttribute("username", username);
        } catch (DisabledAccountException e) {
            errorMsg = e.getMessage();
            modelMap.addAttribute("username", username);
        } catch (IncorrectCredentialsException e) {
            errorMsg = e.getMessage();
            modelMap.addAttribute("username", username);
        } catch (Exception e) {
            errorMsg = "用户登录异常，请联系管理员!";
            e.printStackTrace();
        }
        if (StringUtils.isBlank(errorMsg)) {
            return "redirect:/item/list";
        } else {
            modelMap.addAttribute("errorMsg", errorMsg);
            return "login";
        }
    }

//    @GetMapping("/login")
//    public String login(User user) {
//        Subject subject = SecurityUtils.getSubject();
//        usernamePasswordToken usernamePasswordToken = new usernamePasswordToken(
//                user.getusername(),
//                user.getPassword()
//        );
//        try {
//            subject.login(usernamePasswordToken);
////            subject.checkRole("admin");
////            subject.checkPermissions("query", "add");
//        } catch (AuthenticationException e) {
//            e.printStackTrace();
//            return "账号或密码错误！";
//        } catch (AuthorizationException e) {
//            e.printStackTrace();
//            return "没有权限";
//        }
//        return "login success";
//    }
}
